#include "stdafx.h" 
#include "d3dutil.h"
#include "cGrid.h"


struct VERTEX2
{
	D3DXVECTOR3 pos;
	DWORD color;

	static const DWORD FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
};

cGrid::cGrid(void)
{
	m_pVB = NULL;//grid plan
	m_pVB2 = NULL;//grid xyz
}

cGrid::~cGrid(void)
{
	release();
}


void cGrid::set(float _fspace, float _fcount)
{
	release();

	VERTEX2 vt2;
	std::vector<VERTEX2> m_gridxyz;
	std::vector<VERTEX2> m_gridplan;

	int ncolor = 160;//146;
	float nspace = _fspace;
	float ncount = _fcount;
	float nlenght = nspace*ncount;
	
	//x line
	for(int i = 1; i <= ncount; i++)
	{
		if(i%5 == 0)
		{
			vt2.pos = D3DXVECTOR3(-nlenght, 0, nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(nlenght, 0, nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);
		}
		else
		{
			vt2.pos = D3DXVECTOR3(-nlenght, 0, nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(nlenght, 0, nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);
		}
	}
	for(int i = 1; i <= ncount; i++)
	{
		if(i%5 == 0)
		{
			vt2.pos = D3DXVECTOR3(-nlenght, 0, -nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(nlenght, 0, -nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);
		}
		else
		{
			vt2.pos = D3DXVECTOR3(-nlenght, 0, -nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(nlenght, 0, -nspace*i);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);
		}
	}
	//z line
	for(int i = 1; i <= ncount; i++)
	{
		if(i%5 == 0)
		{
			vt2.pos = D3DXVECTOR3(nspace*i, 0, -nlenght);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(nspace*i, 0, nlenght);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);
		}
		else
		{
			vt2.pos = D3DXVECTOR3(nspace*i, 0, -nlenght);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(nspace*i, 0, nlenght);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);
		}
	}
	for(int i = 1; i <= ncount; i++)
	{
		if(i%5 == 0)
		{
			vt2.pos = D3DXVECTOR3(-nspace*i, 0, -nlenght);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(-nspace*i, 0, nlenght);
			vt2.color = D3DCOLOR_ARGB(0, 0, 0, 0);
			m_gridplan.push_back(vt2);
		}
		else
		{
			vt2.pos = D3DXVECTOR3(-nspace*i, 0, -nlenght);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);

			vt2.pos = D3DXVECTOR3(-nspace*i, 0, nlenght);
			vt2.color = D3DCOLOR_ARGB(0, ncolor, ncolor, ncolor);
			m_gridplan.push_back(vt2);
		}
	}
	////////////////////////////////////////////////////////////////////////////////////
	//create grid xyz
	VERTEX2 vt;
	
	//x
	vt.pos = D3DXVECTOR3(0, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(30, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(25, 0, -5);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(30, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(25, 0, 5);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(30, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);
	//y
	vt.pos = D3DXVECTOR3(0, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 0, 255, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 30, 0);
	vt.color = D3DCOLOR_ARGB(0, 0, 255, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 25, -5);
	vt.color = D3DCOLOR_ARGB(0, 0, 255, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 30, 0);
	vt.color = D3DCOLOR_ARGB(0, 0, 255, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 25, 5);
	vt.color = D3DCOLOR_ARGB(0, 0, 255, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 30, 0);
	vt.color = D3DCOLOR_ARGB(0, 0, 255, 0);
	m_gridxyz.push_back(vt);
	//z
	vt.pos = D3DXVECTOR3(0, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 0, 30);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(-5, 0, 25);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 0, 30);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(5, 0, 25);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 0, 30);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

	//else
	//x
	vt.pos = D3DXVECTOR3(-nlenght, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(nlenght, 0, 0);
	vt.color = D3DCOLOR_ARGB(0, 255, 0, 0);
	m_gridxyz.push_back(vt);
	//z
	vt.pos = D3DXVECTOR3(0, 0, -nlenght);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

	vt.pos = D3DXVECTOR3(0, 0, nlenght);
	vt.color = D3DCOLOR_ARGB(0, 0, 0, 255);
	m_gridxyz.push_back(vt);

//create d3dmesh

	// make gridplan
	{
		HR(gd3dDevice->CreateVertexBuffer((UINT)m_gridplan.size()*sizeof(VERTEX2),
			0, VERTEX2::FVF, D3DPOOL_MANAGED, &m_pVB, NULL));

		VERTEX2* pVertices4;
		HR(m_pVB->Lock(0, 0, (void**)&pVertices4, 0));

		for(UINT i = 0; i < m_gridplan.size(); i++)
		{
			pVertices4[i].pos = m_gridplan[i].pos;
			pVertices4[i].color = m_gridplan[i].color;
		}
		m_pVB->Unlock();
	}
	// make gridxyz
	{
		HR(gd3dDevice->CreateVertexBuffer((UINT)m_gridxyz.size()*sizeof(VERTEX2),
			0, VERTEX2::FVF, D3DPOOL_MANAGED, &m_pVB2, NULL));

		VERTEX2* pVertices4;
		HR(m_pVB2->Lock(0, 0, (void**)&pVertices4, 0));

		for(UINT i = 0; i < m_gridxyz.size(); i++)
		{
			pVertices4[i].pos = m_gridxyz[i].pos;
			pVertices4[i].color = m_gridxyz[i].color;
		}
		m_pVB2->Unlock();
	}

	VB1FaceCount = m_gridplan.size()/2;
	VB2FaceCount = m_gridxyz.size()/2;

}

void cGrid::render(void)
{
	D3DXMATRIX W;
	D3DXMatrixIdentity(&W);
	gd3dDevice->SetTransform(D3DTS_WORLD, &W);

	gd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	gd3dDevice->SetFVF(VERTEX2::FVF);
	gd3dDevice->SetTexture(0, NULL);

	gd3dDevice->SetStreamSource(0, m_pVB, 0, sizeof(VERTEX2));
	gd3dDevice->DrawPrimitive(D3DPT_LINELIST, 0, VB1FaceCount);

	gd3dDevice->SetStreamSource(0, m_pVB2, 0, sizeof(VERTEX2));
	gd3dDevice->DrawPrimitive(D3DPT_LINELIST, 0, VB2FaceCount);
}

void cGrid::release(void)
{
	SAFE_RELEASE(m_pVB);
	SAFE_RELEASE(m_pVB2);
}

